package com.yuwei.parkinglot.view

import android.os.Build
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.view.Gravity
import android.view.MenuItem
import android.view.View
import android.view.View.OnClickListener
import android.widget.PopupMenu
import android.widget.PopupMenu.OnMenuItemClickListener
import androidx.annotation.RequiresApi
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.ToastUtils
import com.tencent.mmkv.MMKV
import com.tencent.smtt.utils.p
import com.yuwei.base.BaseActivity
import com.yuwei.base.path.RouterPath
import com.yuwei.http.entity.ParkingLotEntityItem
import com.yuwei.parkinglot.R
import com.yuwei.parkinglot.adapter.AddParkingStatusAdapter
import com.yuwei.parkinglot.databinding.ActivityAddParkingBinding
import com.yuwei.parkinglot.databinding.ActivityParkingLotBinding
import com.yuwei.parkinglot.viewmodel.ParkingLotViewModel
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
@Route(path = RouterPath.MODULE_ADD_PARKING_ACTIVITY)
class AddParkingActivity : BaseActivity<ActivityAddParkingBinding,ParkingLotViewModel>() {
    @Autowired
    @JvmField
    var parkingLotId:Int = -1
    lateinit var parkingLotEntityItem: ParkingLotEntityItem
    lateinit var parkingLotArea : String
    lateinit var parkingLotType : String
    lateinit var parkingLotStatus : String
    val statusList = listOf("空闲","已占用","维修中")
    val statusAdapter = AddParkingStatusAdapter()
    override fun initView() {
        ARouter.getInstance().inject(this)
        vm.getParkingLotDetail(parkingLotId)
        binding.addParkingBack.setOnClickListener {
            finish()
        }
        binding.addParkingStatusRv.adapter = statusAdapter
        binding.addParkingStatusRv.layoutManager = LinearLayoutManager(this,LinearLayoutManager.HORIZONTAL,false)
        binding.addParkingSave.setOnClickListener { v ->
            if(binding.addParkingNum.text.toString().isEmpty()){
                ToastUtils.showShort("请输入车位编号")
                return@setOnClickListener
            }
            if(binding.addParkingArea.text.toString() == "选择所属区域"){
                ToastUtils.showShort("请选择停车位区域")
                return@setOnClickListener
            }
            if(binding.addParkType.text.toString() == "输入车位类型"){
                ToastUtils.showShort("请选择停车位类型")
                return@setOnClickListener
            }
            if(statusAdapter.getPosition() == -1){
                ToastUtils.showShort("请选择停车位状态")
                return@setOnClickListener
            }
            if(statusAdapter.getPosition() == 1){
                if(binding.addParkingName.text.toString().isEmpty()){
                    ToastUtils.showShort("请输入使用人")
                    return@setOnClickListener
                }
                if(binding.addParkingCarNum.text.toString().isEmpty()){
                    ToastUtils.showShort("请输入车牌号码")
                    return@setOnClickListener
                }
                if(binding.addParkingRemarks.text.toString().isEmpty()){
                    binding.addParkingRemarks.setText("无")
                }
            }
            if (parkingLotId == -1) {
                val parkingLotEntityItem = ParkingLotEntityItem(
                    areaId = 1,
                    areaName = parkingLotArea,
                    id = 0,
                    licensePlate = binding.addParkingCarNum.text.toString(),
                    remarks = binding.addParkingRemarks.text.toString(),
                    spaceNumber = binding.addParkingNum.text.toString(),
                    spaceStatus = parkingLotStatus,
                    spaceType = parkingLotType,
                    userId = MMKV.defaultMMKV().decodeInt("userId"),
                    userName = binding.addParkingName.text.toString()
                )
                vm.addParkingLot(parkingLotEntityItem)
            }else{
                val parkingLotEntityItem = ParkingLotEntityItem(
                    areaId = 1,
                    areaName = parkingLotArea,
                    id = 0,
                    licensePlate = binding.addParkingCarNum.text.toString(),
                    remarks = binding.addParkingRemarks.text.toString(),
                    spaceNumber = binding.addParkingNum.text.toString(),
                    spaceStatus = parkingLotStatus,
                    spaceType = parkingLotType,
                    userId = MMKV.defaultMMKV().decodeInt("userId"),
                    userName = binding.addParkingName.text.toString()
                )
                vm.updateParkingLot(parkingLotId,parkingLotEntityItem)
            }
        }
    }

    @RequiresApi(Build.VERSION_CODES.M)
    override fun initData() {
        vm.getParkingLotDetailSuccess.observe(this) {
            parkingLotArea = it.areaName
            parkingLotType = it.spaceType
            parkingLotStatus = it.spaceStatus
            parkingLotEntityItem = it
            binding.addParkingNum.setText(parkingLotEntityItem.spaceNumber)
            binding.addParkingArea.text = parkingLotEntityItem.areaName
            binding.addParkType.text = parkingLotEntityItem.spaceType
            if(it.spaceStatus == "已占用"){
                binding.addParkingName.setText(parkingLotEntityItem.userName)
                binding.addParkingCarNum.setText(parkingLotEntityItem.licensePlate)
            }
            val i = statusList.indexOf(it.spaceStatus)
            statusAdapter.setPosition(i)
            statusAdapter.notifyDataSetChanged()
            binding.addParkingRemarks.setText(parkingLotEntityItem.remarks)
        }
        statusAdapter.data.addAll(statusList)
        statusAdapter.notifyDataSetChanged()
        val popupMenu = PopupMenu(this, binding.addParkingAreaLl)
        popupMenu.menuInflater.inflate(R.menu.area_menu, popupMenu.menu)
        popupMenu.gravity = Gravity.BOTTOM
        popupMenu.setOnMenuItemClickListener { item ->
            parkingLotArea = item?.title.toString()
            binding.addParkingArea.text = item?.title.toString()
            false
        }
        binding.addParkingAreaLl.setOnClickListener { v ->
            popupMenu.show()
        }
        val popupMenu1 = PopupMenu(this, binding.addParkingTypeLl)
        popupMenu1.menuInflater.inflate(R.menu.type_menu, popupMenu1.menu)
        popupMenu1.gravity = Gravity.BOTTOM
        popupMenu1.setOnMenuItemClickListener { item ->
            parkingLotType = item?.title.toString()
            binding.addParkType.text = item?.title.toString()
            false
        }
        binding.addParkingTypeLl.setOnClickListener { v ->
            popupMenu1.show()
        }
        statusAdapter.setOnItemClickListener { adapter, view, position ->
            parkingLotStatus = statusList[position]
            statusAdapter.setPosition(position)
            statusAdapter.notifyDataSetChanged()
            if(parkingLotStatus !== "已占用"){
                binding.addParkingName.isEnabled = false
                binding.addParkingCarNum.isEnabled = false
                binding.addParkingRemarks.isEnabled = false
                binding.addParkingName.setText("")
                binding.addParkingCarNum.setText("")
                binding.addParkingRemarks.setText("")
            }else{
                binding.addParkingName.isEnabled = true
                binding.addParkingCarNum.isEnabled = true
                binding.addParkingRemarks.isEnabled = true
            }

        }
        vm.addParkingLotSuccess.observe(this){
            finish()
            ToastUtils.showLong("添加成功")
        }
        vm.updateParkingLotSuccess.observe(this){
            finish()
            ToastUtils.showLong("修改成功")
        }
    }

}